package edu.berkeley.cs160.dereknelson.prog2;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class PainterCanvas extends View
implements OnTouchListener {
	private ArrayList<FloatPoint> masterPiece;
	private Paint paint;
	private int currentColor;
	private float strokeWidth;
	
	private class FloatPoint {
		public float x;
		public float y;
		public float r;
		public int color;
		
		public FloatPoint(int c, float x, float y, float r) {
			// Store the color with each point so we can draw
			// multiple colors on the same canvas
			this.color = c;
			this.x = x;
			this.y = y;
			// Also store a radius for each point so we can draw
			// a bunch of different sized dots
			this.r = r;
		}
	}
	
	public PainterCanvas(Context c) {
		super(c);
		
        setFocusable(true);
        setFocusableInTouchMode(true);
		setOnTouchListener(this);
		setBackgroundColor(Color.WHITE);
		
		// Defaults
		masterPiece = new ArrayList<FloatPoint>();
		paint = new Paint();
		paint.setAntiAlias(true);
		currentColor = Color.BLACK;
		strokeWidth = 5.0f;
	}
	
	public void clear() {
		masterPiece = new ArrayList<FloatPoint>();
		invalidate();
	}
	
	public void setBrushSize(int size) {
		strokeWidth = (float)size;
	}
	
	public void setBrushColor(int color) {
		currentColor = color;
	}
	
	public boolean onTouch(View v, MotionEvent e) {
		FloatPoint point = new FloatPoint(currentColor, e.getX(), e.getY(), strokeWidth);
		masterPiece.add(point);
		invalidate();
		return true;
	}

	@Override
	public void onDraw(Canvas c) {
		// Actually draw the points that we've stored
		paint = new Paint();
		for (FloatPoint point : masterPiece) {
			paint.setColor(point.color);
			c.drawCircle(point.x, point.y, point.r, paint);
		}
		invalidate();
	}
}
